package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class BankAcount {
	int money;
	
	private Lock lock;
	private Condition take;
	BankAcount(int money) {
		this.money = money;
		lock = new ReentrantLock();
		take = lock.newCondition();//取钱条件
	}
	
	public int getMoney() {
		return money;
	}
	
	public void addMoney(int money) {
		lock.lock();
		this.money += money;
		take.signalAll();
		lock.unlock();
	}
	
	public void subMoney(int money) {
		lock.lock();
		while (this.money < money) {
			try {
				take.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.money -= money;
		lock.unlock();
	}
}